﻿using Engine;
using Engine.Graphics;
using Game;

namespace SAGhoul.ParticleSystem
{
	public class InwardFireworksParticleSystem : ParticleSystem<InwardFireworksParticleSystem.Particle>
	{
		public class Particle : Game.Particle
		{
			public Vector3 StartPosition;
			public Vector3 TargetPosition;
			public float TimeToLive;
			public float TotalTime;
			public float FadeRate;
			public Color BaseColor;
		}

		public Game.Random m_random = new();
		public Vector3 m_center;
		public float m_radius;
		public int m_particleCount;

		public InwardFireworksParticleSystem(Vector3 center, float radius, int particleCount)
			: base(particleCount)
		{
			m_center = center;
			m_radius = radius;
			m_particleCount = particleCount;

			Texture = ContentManager.Get<Texture2D>("Textures/FireworksParticle");
			TextureSlotsCount = 2;

			// 初始化粒子，从圆周向圆心移动
			InitializeParticles();
		}

		private void InitializeParticles()
		{
			float angleStep = (float)(2 * System.Math.PI) / m_particleCount;

			for (int i = 0; i < m_particleCount && i < Particles.Length; i++)
			{
				Particle particle = Particles[i];
				particle.IsActive = true;

				// 计算圆周上的起始位置
				float angle = i * angleStep + m_random.Float(0f, angleStep);
				Vector3 offset = new(
					m_radius * (float)System.Math.Cos(angle),
					m_random.Float(-0.5f, 0.5f), // 添加一些Y轴的变化
					m_radius * (float)System.Math.Sin(angle)
				);

				particle.StartPosition = m_center + offset;
				particle.Position = particle.StartPosition;
				particle.TargetPosition = m_center; // 目标位置是圆心

				particle.Size = new Vector2(m_random.Float(0.15f, 0.3f));
				// 进一步加快速度，缩短生存时间
				particle.TimeToLive = particle.TotalTime = m_random.Float(0.15f, 0.3f);
				particle.FadeRate = m_random.Float(1f, 3f);

				// 固定为金色
				particle.BaseColor = new Color(255, 215, 0); // 金色
				particle.Color = particle.BaseColor;
				particle.TextureSlot = m_random.Int(0, 3);
			}
		}

		public override bool Simulate(float dt)
		{
			bool hasActiveParticles = false;

			for (int i = 0; i < Particles.Length; i++)
			{
				Particle particle = Particles[i];
				if (!particle.IsActive)
					continue;

				hasActiveParticles = true;
				particle.TimeToLive -= dt;

				if (particle.TimeToLive > 0f)
				{
					// 计算粒子移动进度
					float progress = 1f - (particle.TimeToLive / particle.TotalTime);

					// 使用四次方函数让粒子更快地向中心移动（向心加速效果更明显）
					progress = progress * progress * progress * progress;

					// 在起始位置和目标位置之间插值
					particle.Position = Vector3.Lerp(particle.StartPosition, particle.TargetPosition, progress);

					// 处理淡出效果
					float fade = MathUtils.Min(particle.FadeRate * particle.TimeToLive, 1f);
					particle.Color = particle.BaseColor * fade;

					// 随机闪烁效果
					if (m_random.Float(0f, 1f) < 0.1f)
					{
						particle.Color = Color.Transparent;
					}
					else if (m_random.Float(0f, 1f) < 0.2f)
					{
						particle.TextureSlot = m_random.Int(0, 3);
					}
				}
				else
				{
					particle.IsActive = false;
				}
			}

			return !hasActiveParticles;
		}
	}
}
